import java.util.Arrays;

public class MyArraylist implements IList {

    public int[] elem; //存储数据
    public int usedSize; //记录数组中有效的数据个数

    public MyArraylist() {
        this.elem = new int[Constant.DEFAULT_SIZE];
    }


    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i] + " ");
        }
        System.out.println();
    }

    @Override
    public boolean isFull() {
        return elem.length == usedSize;
    }

    private void grow(){
        this.elem = Arrays.copyOf(elem,elem.length * 2);
    }
    @Override
    public void add(int data) {
        //1.判断顺序表是否满了
        if(isFull()){
            //2.如果满了 则要进行扩容操作
            grow();
        }
        //3.存储数据
        elem[usedSize] = data;
        //4.记录usedSize
        usedSize++;
    }

    private void checkPosInAdd(int pos) {
        if(pos < 0 || pos > usedSize){
            throw new PosIllegalException(Constant.ADD_POS_ILLEGAL);
        }
    }

    @Override
    public void add(int pos, int data) {
        //1.是不是满的
        if(isFull()){
            grow();
        }
        checkPosInAdd(pos);
        //2.判断是不是放在usedSize的位置
        if(pos == usedSize){
            elem[usedSize] = data;
            usedSize++;
            return;
        }
        //3.移动数据
        for (int i = usedSize-1; i >= pos; i--) {
            elem[i+1] = elem[i];
        }
        elem[pos] = data;
        usedSize++;
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    private boolean isEmpty() {
        return usedSize == 0;
    }

    private void checkPosInGet(int pos){
        if(pos < 0 || pos >= usedSize){
            throw new PosIllegalException(Constant.GET_POS_ILLEGAL);
        }
    }

    @Override
    public int get(int pos) {
        if(isEmpty()){
            throw new EmptyListException(Constant.EMPTY_LIST);
        }
        checkPosInGet(pos);
        return elem[pos];
    }

    private void checkPosInSet(int pos){
        if(pos < 0 || pos >= usedSize){
            throw new PosIllegalException(Constant.SET_POS_ILLEGAL);
        }
    }

    @Override
    public void set(int pos, int value) {
        if(isEmpty()){
            throw new EmptyListException(Constant.EMPTY_LIST);
        }
        checkPosInSet(pos);
        elem[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        if(isEmpty()){
            throw new EmptyListException(Constant.EMPTY_LIST);
        }
        //2.查找要删除数据的下标
        int index = indexOf(toRemove);
        if(index == -1){
            System.out.println("没有你要删除的数据...");
            return;
        }
        for (int i = index; i < usedSize-1; i++) {
            elem[i] = elem[i+1];
        }
        usedSize--;
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public void clear() {
        for (int i = 0; i < usedSize; i++) {
            elem[i] = 0;
        }
        usedSize = 0;
    }
}
